using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Diagnostics;

namespace Macaw.Moss2007.SeoSlugsSolution
{
    class SeoSlugsCreatePageControlAdapterFeatureReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
            if (webApplication == null) return;

            SeoSlugsCreatePageAdapterJobDefinition job = (SeoSlugsCreatePageAdapterJobDefinition) webApplication.JobDefinitions[SeoSlugsCreatePageAdapterJobDefinition.JobId];
            if (job != null)
            {
                /*SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    EventLog.WriteEntry(SeoSlugsCreatePageAdapterJobDefinition.EventSource, "Deleting old job", EventLogEntryType.Information);
                });*/

                job.Delete();
            }

            job = new SeoSlugsCreatePageAdapterJobDefinition(SeoSlugsCreatePageAdapterJobDefinition.JobName, webApplication)
            {
                Installing = true,
                Schedule = new SPOneTimeSchedule(DateTime.Now)
            };
            job.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
            if (webApplication == null) return;

            SeoSlugsCreatePageAdapterJobDefinition job = (SeoSlugsCreatePageAdapterJobDefinition)webApplication.JobDefinitions[SeoSlugsCreatePageAdapterJobDefinition.JobId];
            if (job != null)
            {
                /*SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    EventLog.WriteEntry(SeoSlugsCreatePageAdapterJobDefinition.EventSource, "Deleting old job", EventLogEntryType.Information);
                });*/

                job.Delete();
            }

            job = new SeoSlugsCreatePageAdapterJobDefinition(SeoSlugsCreatePageAdapterJobDefinition.JobName, webApplication)
            {
                Installing = false,
                Schedule = new SPOneTimeSchedule(DateTime.Now)
            };
            job.Update();
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            /*if (!EventLog.SourceExists(SeoSlugsCreatePageAdapterJobDefinition.EventSource))
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    EventLog.CreateEventSource(SeoSlugsCreatePageAdapterJobDefinition.EventSource, "Application");
                });
            }*/
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }
    }
}
